<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/28
 * Time: 18:39
 */

namespace app\api\controller\v1;
use app\api\model\Book as BookModel;
use app\api\validate\IDMustBePositiveInt;
use app\api\validate\ISBN;
use app\api\validate\PagingParameter;
use app\api\validate\SearchKey;
use app\lib\Exception\BookException;
use app\lib\Exception\CategoryException;

class Book
{
    public function getByID($id)
    {
        (new IDMustBePositiveInt())->toCheck();
        $book = BookModel::getBookByID($id);
        if (!$book) {
            throw new BookException();
        }
        return $book;
    }

    public function getByISBN($isbn)
    {
        (new ISBN())->toCheck();
        $book = BookModel::getBookByISBN($isbn);
        if (!$book) {
            throw new BookException();
        }
        return $book;
    }

    public function searchKey($key){
        (new SearchKey())->toCheck();

        // 用空格分割的key
        $keyArray = explode(' ', $key);
        if(count($keyArray) > 1){
            array_unshift($keyArray, $key);
            $map['name'] = [];
            $map['author']= [];
            $map['translator'] = [];
            $map['isbn'] = [];
            foreach ($keyArray as $key){
                $condition = ['like','%'.$key.'%'];
                array_push($map['name'], $condition);
                array_push($map['author'], $condition);
                array_push($map['translator'], $condition);
                // $condition = ['=','%'.$key.'%'];
                array_push($map['isbn'], $condition);
            }
        } else{
            $map = [
                'author' => ['like','%'.$key.'%'],
                'name' => ['like','%'.$key.'%'],
                'translator'  => ['like','%'.$key.'%'],
                'isbn' => ['like','%'.$key.'%']
            ];
        }
        return  BookModel::whereOr($map)->select();
    }

    // 以下都返回概要信息，设置visible
    // 获取新书
    // public function getNew($num = 15)
    // {
    //     (new Num())->toCheck();
    //     $books = BookModel::getNewBooks($num);
    //     if ($books->isEmpty()) {
    //         throw new BookException();
    //     }
    //     $books->visible(['id','isbn','name','author','main_img_url','price','discount']);
    //     return $books;
    // }

    public function getList($size = 15, $page = 1){
        (new PagingParameter())->toCheck();
        $pagination = BookModel::getBookList($size, $page );
        if($pagination->isEmpty()){
            return [
                'data'=> [],
                'current_page'=>$pagination->currentPage()
            ];
        }

        $pagination->hidden(['publisher','rate','stock','summary','translator']);
        $data = $pagination->toArray();
        return [
            'data'=> $data,
            'current_page'=>$pagination->currentPage()
        ];
    }

    // 获取某类书籍
    public function getByCategory($id)
    {
        (new IDMustBePositiveInt())->toCheck();
        $books = BookModel::getByCategory($id);
        if ($books->isEmpty()) {
            throw new CategoryException();
        }
        $books->visible(['id','isbn','name','author','main_img_url','price','discount']);
        return $books;
    }
}